//
//  WBTabBarController.swift
//  SainMicroblog
//
//  Created by chenxinju on 16/5/3.
//  Copyright © 2016年 chenxinju-a2. All rights reserved.
//

import UIKit

class WBTabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //var popView = WBpopView()
        
        /* 单独加载控制器属性
        let home = WBHomeViewController()
        
        //设置标题
        home.title = "首页"
        home.tabBarItem.image = UIImage(named: "tabbar_home")
        //设置选中图片
        home.tabBarItem.selectedImage = UIImage(named: "tabbar_home_highlighted")
        //添加子控制器
        addChildViewController(UINavigationController(rootViewController: home))
        */
        
        //1.调用封装加载控制器
        addChildViewController()
        
        //2. 自定义TabBar
        let tabbar = WBTaBar()
        //self.tabBar = tabBar
        
        //使用KVC设置自定义TabBar
        setValue(tabbar, forKey: "tabBar")
        
        
        //-----------闭包跳转作为参数传递
        let tabbarClosure = { [weak self] in
           //跳转pop界面
            WBpopView.testShow(self)

        }
        
        //第三步 给闭包赋值
        tabbar.closure = tabbarClosure
    }
    
    
    
    
    
    
    
    
    //==============================通过抽取一个方法 来实现不同的值的赋值
    func addChildViewController(childController: UIViewController,title:String,image:String ) {
        
        //设置标题
        childController.title = title
        //设置控制器图片
        childController.tabBarItem.image = UIImage(named: image)
        //选中按钮的视图
        childController.tabBarItem.image = UIImage(named: "\(image)_highlighted")
        
        //添加子控制器
        addChildViewController(UINavigationController(rootViewController: childController))
        
    }
    
    
    //============================通过方法的重载对功能的代码的抽取
    func addChildViewController() {
    
        addChildViewController(WBHomeViewController(),title: "首页",image: "tabbar_home")
        addChildViewController(WBMessageController(),title: "消息",image: "tabbar_message_center")
        addChildViewController(WBFindViewController(),title: "发现",image: "tabbar_discover")
        addChildViewController(WBSettingController(),title: "设置",image: "tabbar_profile")
    }

}
